#include <stdio.h>
#include <unistd.h>
#include "utask.h"
#define PREEMPTIVE (0)
void task_func(void *arg)
{
    int i;
    volatile int c,b;
    int y = *((int *)arg);

    for (i=0; i<1000; i++) {
        /* do some work */
        for (c=0; c<1000; c++);
            for (b=0; b<1000; b++);
        /* yield */
        if ((i+1)%y==0) {
            utask_yield();
        }
        /* sleep */
        //usleep(10*1000);
    }
    utask_destroy();
}

int main(int argc, char **argv)
{    
    int t1_yield = 500;
    int t2_yield = 250;

#if !(PREEMPTIVE)
    /* init library using the 
       default cooperative scheduler*/
    utask_init(NULL);
#else
    /* init library using the
       preemptive  scheduler*/
    utask_init(sched_rr_alloc());
#endif

    /* create two tasks */
    utask_create (
        task_func, /* task callback */
        &t1_yield, /* task arguments */
        16384      /* stack size 16k */
    );

    utask_create (
        task_func,/* task callback */
        &t2_yield,/* task arguments */
        16384     /* stack size 16k */
    );

    /* start the scheduler */
    utask_run();
    printf("exiting...\n");
    return 0;
}
